package mrusanov.util

import mrusanov.images.{EagerImage, Image}
import java.awt.geom.AffineTransform
import java.awt.image.AffineTransformOp

object ImageUtil {
  def flipImageHorizontally(image:Image):Image = {
    val bufferedImage = image.bufferedImage
    val tx = AffineTransform.getScaleInstance(-1, 1)
    tx.translate(-bufferedImage.getWidth(null), 0)
    val op = new AffineTransformOp(tx, AffineTransformOp.TYPE_NEAREST_NEIGHBOR)
    val flippedImage = op.filter(bufferedImage, null)
    Image(flippedImage, image.xOffset, image.yOffset, image.alpha)
  }

  def flipImagesHorizontally(images:Vector[Image]) = images.map(flipImageHorizontally(_))

  def flipImageVertically(image:Image):Image = {
    val bufferedImage = image.bufferedImage
    val tx = AffineTransform.getScaleInstance(1, -1)
    tx.translate(0, -bufferedImage.getHeight(null))
    val op = new AffineTransformOp(tx, AffineTransformOp.TYPE_NEAREST_NEIGHBOR)
    val flippedImage = op.filter(bufferedImage, null)
    Image(flippedImage, image.xOffset, image.yOffset, image.alpha)
  }

  def flipImagesVertically(images:Vector[Image]) = images.map(flipImageVertically(_))
}
